import requests
import json
from datas import Token, Constant, Card
from utils.decorators import de_log_time_exceptions
from utils.logger import  log


#获取token
@de_log_time_exceptions
def get_token():
    response = requests.post(url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal",
                             params = {"Content-Type":"application/json; charset=utf-8"},
                             json = {"app_id":"cli_a7c79d000098d00e","app_secret":"YNerJYSGP0uc9iJqBMPw94j8dBA8I1EL"}
                             )
    tenant_access_token = response.json()["tenant_access_token"]
    Token.tenant_access_token = tenant_access_token
    log.debug(f"获取了token：{tenant_access_token}")
    return tenant_access_token


#获取chat_id
@de_log_time_exceptions
def get_chat_id():
    response = requests.get(url = "https://open.feishu.cn/open-apis/im/v1/chats",
                            headers = {"Authorization":f"Bearer {Token.tenant_access_token}"},
                            )
    chat_id = response.json()["data"]["items"][0]["chat_id"]
    log.debug(f"获取了chat_id：{chat_id}")
    return chat_id

#发送卡片
@de_log_time_exceptions
def send_card():
    response = requests.post(url = "	https://open.feishu.cn/open-apis/im/v1/messages",
                              headers = {"Authorization":f"Bearer {Token.tenant_access_token}","Content-Type":"application/json; charset=utf-8"},
                              params={"receive_id_type":"chat_id"},
                              json = {"receive_id":Constant.chat_id_1,"msg_type":"interactive","content": json.dumps({
                                    "type":"template",
                                    "data":{
                                        "template_id":"AAqjzIBhKh6ok",
                                        "template_version_name":"1.0.5"
                                        }
                                      })
                                    }
                                 )
    log.debug("发送了卡片")
    return response

#发送消息
@de_log_time_exceptions
def send_message(text:str):
    response = requests.post(url = "	https://open.feishu.cn/open-apis/im/v1/messages",
                            headers = {"Authorization":f"Bearer {Token.tenant_access_token}","Content-Type":"application/json; charset=utf-8"},
                            params={"receive_id_type":"chat_id"},
                            json = {"receive_id":Constant.chat_id_1,"msg_type":"text","content": json.dumps({"text":text})}
                            )
    log.debug(f"发送了消息：{text}")
    return response

def send_result(*args, **kwargs):

    response = requests.post(url = "	https://open.feishu.cn/open-apis/im/v1/messages",
                            headers = {"Authorization":f"Bearer {Token.tenant_access_token}","Content-Type":"application/json; charset=utf-8"},
                            params={"receive_id_type":"chat_id"},
                            json = {"receive_id":Constant.chat_id_1,"msg_type":"interactive","content": json.dumps(Card(result = args,proj_name= args).card_result)}
                            )

    log.info(response)
    return response




if __name__ == '__main__':
    get_token()
    send_result()